﻿using System;
namespace Validate.Net
{

    /// <summary>
    /// 校验字符串最大长度，可以为null
    /// </summary>
    public class ValidateMaxLengthAttribute : AbstractValidateAttribute
    {
        private int maxLeng;

        public ValidateMaxLengthAttribute(int maxLeng)
        {
            this.maxLeng = maxLeng;
        }
        public override bool Validate(object value, out string msg)
        {
            if (value == null)
            {
                msg = string.Empty;
                return true;
            }
            if (!object.ReferenceEquals(typeof(string),value.GetType()))
            {
                msg = $"特性[{this.GetType().Name}]只能校验数字类型";
                return false;
            }
            if (value.ToString().Trim().Length <= this.maxLeng)
            {
                msg = string.Empty;
                return true;
            }
            else{
                msg = $"最大长度为：{this.maxLeng}";
                return false;
            }
        }
    }
}
